package routers

import (
	"gin-ebook/Controllers/Auth"
	"gin-ebook/Middlewares/handler"
	"gin-ebook/Middlewares/jwt"
	_ "gin-ebook/docs"
	"gin-ebook/pkg/setting"
	"github.com/gin-gonic/gin"
	ginSwagger "github.com/swaggo/gin-swagger"
	"github.com/swaggo/gin-swagger/swaggerFiles"

	"gin-ebook/Controllers/Article"
	"gin-ebook/Controllers/Dynamic"
	"gin-ebook/Controllers/Menu"
	"gin-ebook/Controllers/Tag"
)

func InitRouter() *gin.Engine {

	r := gin.New()
	r.Use(gin.Logger())
	r.Use(gin.Recovery())
	r.Use(handler.Cors())
	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
	//apiV1.Use(jwt.JWT())
	//apiV1.Use(handler.Cors())
	gin.SetMode(setting.RunMode)

	// 后端接口
	r2 := r.Group("api/")
	user := r2.Group("/user")
	{
		user.POST("/login", Auth.GetAuth)
		user.POST("/register", Auth.AddAuth)
	}

	tag := r2.Group("/tag")
	{
		tag.GET("/getTags", Tag.GetTags)
		tag.POST("/addTag", Tag.AddTag)
		tag.PUT("editTag/:id", Tag.EditTag)
		tag.DELETE("deleteTag/:id", Tag.DeleteTag)
	}
	user.Use(handler.Cors())
	user.Use(jwt.JWT())
	article := r2.Group("article")
	{
		article.GET("/getArticle/:id", Article.GetArticle)
		article.GET("/getCategoryBooks", Article.GetCategoryBooks)
		article.GET("/getHotArticle", Article.GetHotArticles)
		article.GET("/getSearchBook", Article.SearchBook)
		article.GET("/getRecommendBooks", Article.GetRecommendBooks)
		article.GET("/getBookDetail/:id", Article.GetBookDetail)
		article.GET("/getArticles", Article.GetArticles)
		article.POST("/addArticle", Article.AddArticle)
		article.PUT("/editArticle/:id", Article.EditArticle)
		article.DELETE("/deleteArticle/:id", Article.DeleteArticle)
	}

	menu := r2.Group("menu")
	{
		menu.GET("/getMenu", Menu.GetMenu)
		menu.GET("/getMenuTree", Menu.GetMenuTree)
	}
	dynamic := r2.Group("dynamic")
	{
		dynamic.GET("/getDynamic", Dynamic.GetDynamic)
	}

	// 前端接口
	r3 := r.Group("v1")
	tags := r3.Group("/tag")
	{
		tags.GET("/getTags", Tag.GetTags)
	}
	articles := r3.Group("/article")
	{
		articles.GET("/getArticle/:id", Article.GetArticle)
		articles.GET("/getArticles", Article.GetArticles)
	}
	return r
}
